import 'dart:io';

import 'package:incidentmanager/common/config/constant.dart';

class WebSocketManager {
  final String _token;

  final Function _onOpen;

  WebSocketManager._(this._token, this._onOpen);

  static WebSocketManager _manager;

  factory WebSocketManager(String token, Function onOpen) {
    if (_manager == null) {
      _manager = WebSocketManager._(token, onOpen);
    }
    return _manager;
  }

  WebSocket _webSocket;

  Future<void> initWebSocket() async {
    var url = '$WS_BASE_URL?token=$_token';
    _webSocket = await WebSocket.connect(url);
    _webSocket.listen(_onOpen);
  }

  Future<void> close([int code, String reason]) async {
    await _webSocket.close(code, reason);
  }
}
